package com.mrent.web.controller.manager;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.Member;
import com.mrent.entity.OrderManager;
import com.mrent.service.MemberService;
import com.mrent.service.OrderManagerService;
import com.mrent.util.Constance;
import com.mrent.util.RenderUtil;

@Controller
public class ManagerController {
	
	@Autowired
	private OrderManagerService orderManagerService;
	
	@Autowired
	private MemberService memberService;
	
	/**
	 *  跳转店主登录.
	 *  
	 * @return
	 */
	@RequestMapping("/login/manager")
	public String proxyLogin(){
		
		return "manager/login";
	}
	
	/**
	 * 店主登录.
	 * 
	 * @param orderManager
	 * @return
	 */
	@RequestMapping("/login/manager/go")
	public String login(OrderManager orderManager,String userPassword,HttpServletResponse response,HttpSession session){
		Calendar c = Calendar.getInstance();
		Integer year = c.get(Calendar.YEAR);
		Integer month = c.get(Calendar.MONTH);
		Integer day = c.get(Calendar.DATE);
		
		List<OrderManager> list = orderManagerService.getManagerByMemberDate(orderManager.getOrderMember(), year, month, day);
		if(list.size() <= 0){
			Map<String,Object> json = new HashMap<String,Object>();
			json.put("result", "Faile");
			json.put("ErrorCode", "-1");
			RenderUtil.renderJson(json,response);
		}else{
			OrderManager om = list.get(0);
			Member member = memberService.findByMemberName(orderManager.getOrderMember());
			if(DigestUtils.shaHex(userPassword).equals(member.getMemberPwd())){
				if(orderManager.getLoginPwd().equals(om.getLoginPwd())){
					//会话保存管理员信息.
					session.setAttribute(Constance.SESSION_MANAGER_NAME, om);
					
					Map<String,Object> json = new HashMap<String,Object>();
					json.put("result", "Success");
					RenderUtil.renderJson(json,response);
				}else{
					Map<String,Object> json = new HashMap<String,Object>();
					json.put("result", "Faile");
					json.put("ErrorCode", "-3");
					RenderUtil.renderJson(json,response);
				}
			}else{
				Map<String,Object> json = new HashMap<String,Object>();
				json.put("result", "Faile");
				json.put("ErrorCode", "-2");
				RenderUtil.renderJson(json,response);
			}
		}
		
		
		
		return null;
	}
	
	/**
	 * 店主首页.
	 * 
	 * @return
	 */
	@RequestMapping("/manager/index")
	public String index(){
		
		return "manager/index";
	}
	
	

}
